***************
* Title: gambia_ecd_edcc_clean1.do
* Author: Todd Pugatch
* Description: replication code for Blimpo, Carneiro, Jervis, and Pugatch,
*	"Improving Access and Quality in Early Childhood Development Programs: 
*		Experimental Evidence from The Gambia"
*	for Economic Development and Cultural Change
* Inputs: ECD_3to6_Gambia_stata12.dta, gambia_ecd_clean1_aux_[base/end]line.do, census_settlement.dta, 
*	irt_output_clean3.dta
* Outputs: ECD_3to6_Gambia_cleanv1.dta
* Notes: creates main dataset used in paper
****************
#delimit;
local start=`"$S_TIME"';
clear;
clear matrix;
clear mata;
graph drop _all;
cap log close;
set more off;
/*set directory:
	cd mydir
*/
local data=`"Data"';
local baseline=`"Data\Baseline"';
local endline=`"Data\Endline"';
local cleandata=`"Data\cleaned"';
local do=`"analysis\do-files"';
qui use `cleandata'\ECD_3to6_Gambia_stata12, clear;

* CLEAN BASELINE DATA;
* fix region and district variables;
/*"_code" suffix is baseline, "region"/"district" is baseline*/
/*attrition and new entrants in endline mean these don't always match*/
/*create single variable for both, but later need to define in/out of baseline/endline for sample*/
/*note that variable "settlement_code" is complete*/
foreach x in region district {;
	assert `x'==`x'_code if `x'!=. & `x'_code!=.; /*verify that these match for those in both waves*/
	qui egen `x'x=rowmax(`x'_code `x');
	drop `x' `x'_code;
	ren `x'x `x';
};
lab val region ip2;
lab val district ip4;

* region dummies;
foreach r in 2 6 {;
	qui gen region`r'=(region==`r');
	lab var region`r' "Region `r'";
};

* treatment;
* WHY DO TREATMENT AND IP9 VARIABLES DIFFER?;
lab def treatment 1 "control (ages 3-6)" 4 "ECD Annex control" 5 "ECD Annex treated" 6 "ECD Community-based";
lab val treatment treatment;

* child age in months;
/*approximate if reported in years*/
qui gen child_age_mths=child_age if child_age_unit==2;
qui replace child_age_mths=child_age*12 if child_age_unit==1;
qui gen child_age_mths_impute=(child_age!=. & child_age_unit==1);
lab var child_age_mths "child age in months";
lab var child_age_mths_impute "child age in months imputed from age in years";

/*age based on date of birth*/
qui tostring ip16, gen(baselinedate_str) format(%20.0f);
qui replace baselinedate_str = "0" + baselinedate_str if length(baselinedate_str) == 7;
qui gen baselinedate = date(baselinedate_str,"DMY");
qui gen dob = mdy(s7q06_month,s7q06_day,s7q06_year);
format dob %d;
format baselinedate %d;
personage dob baselinedate, gen(ageyrs days);
qui gen child_age_mths_dob=12*(ageyrs + (days/366)); /*use 366 days since 2012 (baseline) was a leap year*/
qui gen child_age_yrs_dob=ageyrs + (days/366);
drop ageyrs days baselinedate_str;
lab var dob "date of birth (reported at baseline, Stata format)";
lab var baselinedate "date of baseline interview (Stata format)";
lab var child_age_mths_dob "child age in months at baseline, based on date of birth";
lab var child_age_yrs_dob "child age in years at baseline, based on date of birth";

/*assess quality of age based on DOB*/
corr child_age_mths child_age_mths_dob selected_child_age;
corr child_age_mths child_age_mths_dob selected_child_age if child_age_mths_impute==0;
qui gen showed_birthcertificate=(s7q08==1);
qui replace showed_birthcertificate=. if child_age_mths_dob==.;
qui gen diff_bl=abs(child_age_mths_dob-child_age_mths);
qui gen diff_el1=selected_child_age-child_age_mths;
qui gen diff_el2=selected_child_age-child_age_mths_dob;
qui gen within_onemonth_bl=(diff_bl<1);
foreach x in el1 el2 {;
	qui gen within_16to18months_`x'=(diff_`x'>=16 & diff_`x'<=18);
};
qui replace within_onemonth_bl=. if child_age_mths_impute==1|child_age_mths_dob==.|child_age_mths==.;
qui replace within_16to18months_el1=. if child_age_mths_impute==1|child_age_mths==.|selected_child_age==.;
qui replace within_16to18months_el2=. if child_age_mths_dob==.|selected_child_age==.;
lab var showed_birthcertificate "Caregiver showed selected child's birth certificate at baseline";
lab var within_onemonth_bl "Difference in child age within one month at baseline, reported v. DOB measure";
lab var within_16to18months_el1 "Difference in child age within 16-18 months baseline v. endline, reported baseline measure";
lab var within_16to18months_el2 "Difference in child age within 16-18 months baseline v. endline, DOB baseline measure";
lab var diff_bl "Difference in child age in months at baseline, reported v. DOB measure";
lab var diff_el1 "Difference in child age in months baseline v. endline, reported baseline measure";
lab var diff_el2 "Difference in child age in months baseline v. endline, DOB baseline measure";
su showed_birthcertificate within_* diff_*;
foreach x in bl el1 el2 {;
	su diff_`x', d;
};
/*summary of evidence here:
	--reported and DOB baseline age measures within one month 90% of the time. 
	--correlation between reported and DOB baseline age = .87 (.70 if excluding those baseline reported in years)
	--2/3 reporting a DOB showed birth certificate
	--correlation between baseline reported age and endline age: .81 (.64 if excluding those baseline reported in years)
	--correlation between baseline DOB age and endline age: .87 (.64 if excluding those baseline reported in years)
	--median difference between baseline reported age and endline age = 18 months
	--median difference between baseline DOB age and endline age = 17.3 months
	*/
drop showed_birthcertificate within_* diff_*;
 
* child sex;
/*use baseline sex as default, but flag mismatched sex between endline and baseline*/
qui gen child_female=.;
qui replace child_female=1 if child_sex==2 & (selected_child_gender==2|selected_child_gender==.); /*baseline & endline gender match*/
qui replace child_female=1 if selected_child_gender==2 & (child_sex==2|child_sex==.);
qui replace child_female=0 if child_sex==1 & (selected_child_gender==1|selected_child_gender==.); /*baseline & endline gender match*/
qui replace child_female=0 if selected_child_gender==1 & (child_sex==1|child_sex==.);
qui gen child_gender_mismatch=((child_sex==1 & selected_child_gender==2)|(child_sex==2 & selected_child_gender==1));
/*enable next lines to use baseline sex when baseline & endline in conflict*/
qui replace child_female=child_sex-1 if child_gender_mismatch==1;
lab var child_gender_mismatch "Child gender mismatched between baseline and endline";

* Do children with mismatched gender also have mismatched names?;
/*fix names manually when first reported sex appears wrong*/
/*note that almost all have same name (except for spelling and capitalization), so merge is probably fine*/
/*check names against common Gambia names: http://www.accessgambia.com/information/names.html, 
	http://www.columbia.edu/~msj42/Common%20Gambian%20First%20Names.htm#S,
	http://resourcepage.gambia.dk/names.htm*/

/*mismatched names between baseline and endline*/	
list selected_child selected_child_s_name child_female if child_gender_mismatch==1 & selected_child!=selected_child_s_name;	
qui replace child_female=0 if selected_child=="essa gaye"; 
qui replace child_female=1 if selected_child=="MAIMUNA KOLLEY"; 
qui replace child_female=1 if selected_child=="HAMNA JAWO";  
qui replace child_female=1 if selected_child=="ALIMA KEBBEH";  
qui replace child_female=1 if selected_child=="JANNA JARGURAGA"; 
qui replace child_female=0 if selected_child=="Ousman Darboe"; 
qui replace child_female=0 if selected_child=="Ousman Jallow";  
qui replace child_female=0 if selected_child=="Muhammadou Jawara"; 
qui replace child_female=0 if selected_child=="Mustapha Jawara"; 
qui replace child_female=1 if selected_child=="Fatoumata susso"; 
qui replace child_female=1 if selected_child=="JARIATOU SANKANO";
qui replace child_female=1 if selected_child=="fatoumata wally";  
qui replace child_female=0 if selected_child=="BAKADDI KANTEH";
qui replace child_female=0 if selected_child=="GUDO SAHO";
qui replace child_female=1 if selected_child=="AMINATA TOURAY";

/*matched names between baseline and endline*/
sort selected_child;
list selected_child selected_child_s_name child_female if child_gender_mismatch==1 & selected_child==selected_child_s_name;
qui replace child_female=1 if selected_child=="ADAMA JALLOW";
qui replace child_female=0 if selected_child=="ALFUSAINEY SANYANG";
qui replace child_female=1 if selected_child=="ALIMATOU SANYANG";
qui replace child_female=0 if selected_child=="EBRIMA SILLAH";
qui replace child_female=1 if selected_child=="FATIMA DRAMMEH";
qui replace child_female=1 if selected_child=="ISATOU BOJANG";
qui replace child_female=1 if selected_child=="JAINABA CAMARA";
qui replace child_female=1 if selected_child=="JANNA DAMBA";
qui replace child_female=1 if selected_child=="KADDY JAWO";
qui replace child_female=1 if selected_child=="KUMBA BAH";
qui replace child_female=1 if selected_child=="KUMBA SANNEH";
qui replace child_female=0 if selected_child=="MAMADI SANYANG";
qui replace child_female=1 if selected_child=="MANSATA CAMARA";
qui replace child_female=1 if selected_child=="MARIAMA BAH";
qui replace child_female=1 if selected_child=="NYIMA KOLLEY";
qui replace child_female=1 if selected_child=="ROHEY NJIE";
qui replace child_female=0 if selected_child=="SANNA JANKO";
qui replace child_female=0 if selected_child=="TIJAN SOWE";

/*questionable combinations:
	SALLEY JANNEH          BAKARY  JANNEH
	AMINATA BAH               TIJAN BAH
	LAMIN KAMARTEH      FATOUMATTA KAMATEH
	SARJO JAMMEH (unisex name)*/
qui gen child_gender_mismatch_resolved=.;
qui replace child_gender_mismatch_resolved=1 if child_gender_mismatch==1;
qui replace child_gender_mismatch_resolved=0 if selected_child=="SALLEY JANNEH" & child_gender_mismatch==1; 
qui replace child_gender_mismatch_resolved=0 if selected_child=="AMINATA BAH" & child_gender_mismatch==1; 
qui replace child_gender_mismatch_resolved=0 if selected_child=="LAMIN KAMARTEH" & child_gender_mismatch==1; 
*qui replace child_gender_mismatch_resolved=0 if selected_child=="SARJO JAMMEH" & child_gender_mismatch==1; /*Leave this one b/c it is the same child. Go with baseline reported sex.*/  
qui gen child_gender_modified=child_female+1;
lab val child_gender_modified SELECTED_CHILD_GENDER;
lab var child_gender_modified "Child gender, baseline/endline (baseline when in conflict)";
lab var child_gender_mismatch_resolved "gender mismatched resolved by viewing child names";

* number of other children in household (choose 14 as cutoff age because endline asks about children up to 13);
/*see Section 7 for children born to mother*/
forval i=1/9 {;
	qui gen under14_`i'=(s1q04_0`i'<14 & s1q04_0`i'!=.);
};
forval i=10/40 {;
	qui gen under14_`i'=(s1q04_`i'<14 & s1q04_`i'!=.);
};
order under14_*;
qui egen numchildrenhh_baseline=rowtotal(under14_1-under14_40);
qui replace numchildrenhh_baseline=numchildrenhh_baseline-1; /*exclude selected child*/
drop under14_*;
lab var numchildrenhh_baseline "number of children under 14 in household (excl. selected child), baseline";

* polygamous household: code as polygamous if more than one HH member identified as wife of HH head;
forval i=1/9 {;
	qui gen wife_`i'=(s1q02_0`i'==2 & s1q03_0`i'==2);
};
qui egen numwives=rowtotal(wife_1-wife_9);
qui gen polygamous=(numwives>1 & numwives!=.);
drop wife_?;
lab var numwives "number of wives in household";
lab var polygamous "polygamous household";

* Module 2: schooling;
* child baseline preschool attendance and mother tongue;
foreach x in preschlattend_baseline schlever_baseline schlattend_baseline mothertongue {;
	qui gen child_`x'=.;
};
forval i=1/9 {;
	qui replace child_preschlattend_baseline=(s2q03a_0`i'==1) if selected_child_code==`i';
	qui replace child_schlever_baseline=(s2q04_0`i'==1) if selected_child_code==`i';
	qui replace child_mothertongue=s2q01_0`i' if selected_child_code==`i';

};
forval i=10/40 {;
	qui replace child_preschlattend_baseline=(s2q03a_`i'==1) if selected_child_code==`i';
	qui replace child_schlever_baseline=(s2q04_`i'==1) if selected_child_code==`i';
	qui replace child_mothertongue=s2q01_`i' if selected_child_code==`i';
};
lab var child_preschlattend_baseline "child had attended pre-school at baseline";
lab var child_schlever_baseline "child had attended school at baseline";
lab def mothertongue 1 "Mandinka/Jahanka" 2 "Fula/Tukulorr" 3 "Wolof" 4 "Jola/Karoninka" 5 "Serahule" 6 "Serere"
	7 "Creole/Aku Marabout" 8 "Manjago" 9 "Non-Gambian" 96 "Other";
lab val child_mothertongue mothertongue;
lab var child_mothertongue "child's mother tongue";	

* WHY DO SOME CHILDREN WITH child_tested==0 have test scores?;

* HH size: hh_total;

* mother's schooling and marital status;
qui gen mother_schl_ever=.;
qui gen mother_schl_yrs=.;
qui gen mother_readorwrite=.;
qui gen mother_married=.;
forval i=1/9 {;
	qui replace mother_schl_ever=(s2q04_0`i'==1) if mother_code_final==`i';
	qui replace mother_schl_yrs=s2q06_0`i' if mother_code_final==`i';
	qui replace mother_readorwrite=(s2q10_0`i'==1|s2q10_0`i'==2|s2q10_0`i'==3) if mother_code_final==`i';
	qui replace mother_married=(s2q05_0`i'==1) if mother_code_final==`i';
};
forval i=10/40 {;
	qui replace mother_schl_ever=(s2q04_`i'==1) if mother_code_final==`i';
	qui replace mother_schl_yrs=s2q06_`i' if mother_code_final==`i';
	qui replace mother_married=(s2q05_`i'==1) if mother_code_final==`i';
};
qui replace mother_schl_yrs=0 if mother_schl_ever==0;
qui replace mother_schl_yrs=. if mother_schl_yrs==99;
lab var mother_schl_ever "mother ever attended school (based on mother_code_final)";
lab var mother_schl_yrs "mother schooling in years (based on mother_code_final)";
lab var mother_readorwrite "mother can read or write (any language, based on mother_code_final)";
lab var mother_married "mother married (based on mother_code_final)";

* head of HH schooling;
qui gen headhh_schl_ever=.;
qui gen headhh_schl_yrs=.;
qui gen headhh_readorwrite=.;
qui replace headhh_schl_ever=(s2q04_01==1) if s1q02_01==1;
qui replace headhh_schl_yrs=s2q06_01 if s1q02_01==1;
qui replace headhh_readorwrite=(s2q10_01==1|s2q10_01==2|s2q10_01==3) if s1q02_01==1;
qui replace headhh_schl_yrs=0 if headhh_schl_ever==0;
qui replace headhh_schl_yrs=. if headhh_schl_yrs==99;
lab var headhh_schl_ever "head of household ever attended school";
lab var headhh_schl_yrs "head of household schooling in years";
lab var headhh_readorwrite "head of household can read or write in any language";

* Module 3: labor;
* head of HH labor;
foreach x in headhh_hrswrk headhh_sector {;
	qui gen `x'=.;
};
qui replace headhh_hrswrk=s3q01_1 if s3q00_1==1;
qui replace headhh_sector=s3q10_1 if s3q00_1==1;
qui gen headhh_hrswrk_cat=.;
qui replace headhh_hrswrk_cat=1 if headhh_hrswrk==0;
qui replace headhh_hrswrk_cat=2 if headhh_hrswrk>0 & headhh_hrswrk<=20 & headhh_hrswrk!=.;
qui replace headhh_hrswrk_cat=3 if headhh_hrswrk>20 & headhh_hrswrk<=40 & headhh_hrswrk!=.;
qui replace headhh_hrswrk_cat=4 if headhh_hrswrk>40 & headhh_hrswrk<=60 & headhh_hrswrk!=.;
qui replace headhh_hrswrk_cat=5 if headhh_hrswrk>=60 & headhh_hrswrk!=.;
qui gen headhh_work=(headhh_hrswrk>0 & headhh_hrswrk!=. & s3q00_1==1);
qui replace headhh_work=. if headhh_hrswrk==.;
qui gen headhh_ag=(headhh_sector==1);
qui replace headhh_ag=. if s3q10_1==.|s3q00_1!=1;
lab def headhh_hrswrk_cat 1 "none" 2 "1-20 hours" 3 "21-40 hours" 4 "41-60 hours" 5 "more than 60 hours";
lab val headhh_hrswrk_cat headhh_hrswrk_cat;
lab val headhh_sector s3q10_1;
lab var headhh_hrswrk "hours worked for pay last week, head of household (baseline)";
lab var headhh_hrswrk_cat "hours worked for pay last week (categorical), head of household (baseline)";
lab var headhh_work "head of household worked for pay last week (baseline)";
lab var headhh_sector "sector of work, head of household (baseline)";
lab var headhh_ag "head of household works in agriculture (baseline)";

* mother or main caregiver labor;
qui gen mother_baseline=(s3q00_2==mother_code);
qui gen mother_head_baseline=(mother_code==1|mother_code==2 & s1q02_02==1);
foreach x in mother_hrswrk mother_sector {;
	qui gen `x'=.;
};
qui replace mother_hrswrk=s3q01_2 if s3q00_2!=.;
qui replace mother_hrswrk=s3q01_1 if mother_head_baseline==1;
qui replace mother_sector=s3q10_2 if s3q00_2!=.;
qui replace mother_sector=s3q10_1 if mother_head_baseline==1;
qui gen mother_hrswrk_cat=.;
qui replace mother_hrswrk_cat=1 if mother_hrswrk==0;
qui replace mother_hrswrk_cat=2 if mother_hrswrk>0 & mother_hrswrk<=20 & mother_hrswrk!=.;
qui replace mother_hrswrk_cat=3 if mother_hrswrk>20 & mother_hrswrk<=40 & mother_hrswrk!=.;
qui replace mother_hrswrk_cat=4 if mother_hrswrk>40 & mother_hrswrk<=60 & mother_hrswrk!=.;
qui replace mother_hrswrk_cat=5 if mother_hrswrk>=60 & mother_hrswrk!=.;
qui gen mother_work=(mother_hrswrk>0 & mother_hrswrk!=. & s3q00_2!=.);
qui replace mother_work=1 if mother_hrswrk>0 & mother_hrswrk!=. & mother_head_baseline==1;
qui replace mother_work=. if mother_hrswrk==.;
qui gen mother_ag=(mother_sector==1);
qui replace mother_ag=. if s3q10_2==.|s3q00_2==.|(s3q10_2==. & mother_head_baseline==1);
lab def mother_hrswrk_cat 1 "none" 2 "1-20 hours" 3 "21-40 hours" 4 "41-60 hours" 5 "more than 60 hours";
lab val mother_hrswrk_cat mother_hrswrk_cat;
lab val mother_sector s3q10_2;
lab var mother_baseline "respondent to mother baseline labor questions is mother, not different caregiver";
lab var mother_head_baseline "mother is head of household, baseline";
lab var mother_hrswrk "hours worked for pay last week, mother or caregiver (baseline)";
lab var mother_hrswrk_cat "hours worked for pay last week (categorical), mother or caregiver (baseline)";
lab var mother_work "mother or caregiver worked for pay last week (baseline)";
lab var mother_sector "sector of work, mother or caregiver (baseline)";
lab var mother_ag "mother or caregiver works in agriculture (baseline)";
qui gen mother_unpaid=.;
qui replace mother_unpaid=1 if (s3q02_2==1|s3q02_2==2) & s3q00_2!=.;
qui replace mother_unpaid=1 if (s3q02_1==1|s3q02_2==1) & mother_head_baseline==1;
qui replace mother_unpaid=0 if s3q02_2==3 & s3q00_2!=.;
qui replace mother_unpaid=0 if s3q02_1==3 & mother_head_baseline==1;
lab var mother_unpaid "mother does unpaid work (baseline)";

* Module 4: expenditure;
* daily expenditures;
order s4q02* s4q03* s4q04*;
qui egen expend_daily=rowtotal(s4q02_01-s4q04_13);

* monthly and yearly expenditures;
/*first sort items into monthly v. yearly*/
forval i=1/7 {;
	qui gen monthly`i'=(s4q20_`i'>=201 & s4q20_`i'<=208 & s4q20_`i'!=.);
	qui gen yearly`i'=(s4q20_`i'>=209 & s4q20_`i'<=215 & s4q20_`i'!=.);
};
/*now dummy expenditures into/out of monthly/yearly totals*/
forval i=1/7 {;
	qui gen monthlyexp`i'=monthly`i'*(s4q05_`i'+s4q06_`i'+s4q07_`i'+s4q08_`i');
	qui gen yearlyexp`i'=yearly`i'*(s4q05_`i'+s4q06_`i'+s4q07_`i'+s4q08_`i');
};
order monthlyexp? yearlyexp?;
foreach x in monthly yearly {;
	qui egen expend_`x'=rowtotal(`x'1-`x'7);
};
drop monthly* yearly*;

* aggregate annual expenditure and get per capita HH measure (code follows ecd_gambia_moussa_old.do, line 1047);
qui gen expend_yr_hhpc = (12*(30*expend_daily + expend_monthly) + expend_yearly)/hh_total;
qui gen expend_yr_hhpc_usd=expend_yr_hhpc/30.3035; /*GMD/USD exchange rate in June 2012, http://www.exchangerates.org.uk/USD-GMD-05_06_2012-exchange-rate-history.html*/
foreach x in daily monthly yearly {;
	lab var expend_`x' "`x' HH expenditure (GMD, baseline)";
};
lab var expend_yr_hhpc "annual household expenditure per capita (GMD, baseline)";
lab var expend_yr_hhpc_usd "annual household expenditure per capita (USD, baseline)";

* Module 5: assets;
/*lots of stuff here, just use a subset*/
foreach x in dirtfloor thatchroof {;
	qui gen `x'=.;
};
qui replace dirtfloor=1 if s5q08<=4 & s5q08!=.;
qui replace dirtfloor=0 if s5q08>4 & s5q08!=.;
qui replace thatchroof=1 if s5q10<=3 & s5q10!=.;
qui replace thatchroof=0 if s5q10>3 & s5q10!=.;
lab var dirtfloor "household floor made of dirt or other natural material, baseline";
lab var thatchroof "household roof made of thatch or other natural material, baseline";

/*find first principal component of asset inventory*/
forval i=1/11 {;
	qui gen alt_s5q14_`i'=s5q14_`i';
	qui replace alt_s5q14_`i'=0 if s5q14_`i'==2;
};
forval i=1/7 {;
	qui gen alt_s5q15_`i'=s5q15_`i';
	qui replace alt_s5q15_`i'=0 if s5q15_`i'==2;
};
forval i=1/5 {;
	qui gen alt_s5q16_`i'=s5q16_`i';
	qui replace alt_s5q16_`i'=0 if s5q16_`i'==2;
};
forval i=1/2 {;
	qui gen alt_s5q17_`i'=s5q17_`i';
	qui replace alt_s5q17_`i'=0 if s5q17_`i'==2;
};
order alt_s5q14* alt_s5q15* alt_s5q16* alt_s5q17*;
qui pca alt_s5q14_1-alt_s5q17_2;
qui predict pc1;
drop alt*;
lab var pc1 "first principal component of household assets";

* Module 6: demand for ECD;
/*be sure to condition on presence of child 3-6 in HH*/
/*attendance*/
foreach x in attend annex madrassa community private other wtp wtpq {;
	qui gen ecd_`x'_baseline=.;
};
qui replace ecd_attend_baseline=1 if s6q09==1 & s6q08==1;
qui replace ecd_attend_baseline=0 if s6q09==2 & s6q08==1;
qui replace ecd_annex_baseline=1 if s6q10_1==1 & ecd_attend_baseline==1 & s6q08==1;
qui replace ecd_annex_baseline=0 if s6q10_1!=1 & (ecd_attend_baseline==1|ecd_attend_baseline==0) & s6q08==1;
qui replace ecd_madrassa_baseline=1 if (s6q10_2==1|s6q10_5==1) & ecd_attend_baseline==1 & s6q08==1;
qui replace ecd_madrassa_baseline=0 if (s6q10_2!=1 & s6q10_5!=1) & (ecd_attend_baseline==1|ecd_attend_baseline==0) & s6q08==1;
qui replace ecd_community_baseline=1 if s6q10_3==1 & ecd_attend_baseline==1 & s6q08==1;
qui replace ecd_community_baseline=0 if s6q10_3!=1 & (ecd_attend_baseline==1|ecd_attend_baseline==0) & s6q08==1;
qui replace ecd_private_baseline=1 if s6q10_4==1 & ecd_attend_baseline==1 & s6q08==1;
qui replace ecd_private_baseline=0 if s6q10_4!=1 & (ecd_attend_baseline==1|ecd_attend_baseline==0) & s6q08==1;
qui replace ecd_other_baseline=1 if s6q10_6==1 & ecd_attend_baseline==1 & s6q08==1;
qui replace ecd_other_baseline=0 if s6q10_6!=1 & (ecd_attend_baseline==1|ecd_attend_baseline==0) & s6q08==1;
lab var ecd_attend_baseline "Attends ECD at baseline";
foreach x in annex community private other {;
	lab var ecd_`x'_baseline "Attends `x' ECD at baseline";
};
lab var ecd_madrassa_baseline "Attends madrassa or daara ECD at baseline";

/*reason for no attendance*/
foreach x in tooyoung notavail tooexp noneed other {;
	qui gen ecd_noattnd_`x'_bl=.;
};
qui replace ecd_noattnd_tooyoung_bl=1 if s6q11==1 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_tooyoung_bl=0 if s6q11!=1 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_notavail_bl=1 if s6q11==2 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_notavail_bl=0 if s6q11!=2 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_tooexp_bl=1 if s6q11==3 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_tooexp_bl=0 if s6q11!=3 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_noneed_bl=1 if s6q11==4 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_noneed_bl=0 if s6q11!=4 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_other_bl=1 if s6q11==6 & ecd_attend_baseline==0 & s6q08==1;
qui replace ecd_noattnd_other_bl=0 if s6q11!=6 & ecd_attend_baseline==0 & s6q08==1;
lab var ecd_noattnd_tooyoung_bl "Don't attend ECD at baseline because child too young";
lab var ecd_noattnd_notavail_bl "Don't attend ECD at baseline because facility not available";
lab var ecd_noattnd_tooexp_bl "Don't attend ECD at baseline because too expensive";
lab var ecd_noattnd_noneed_bl "Don't attend ECD at baseline because don't see need";
lab var ecd_noattnd_other_bl "Don't attend ECD at baseline because other reason";

/*willingness to pay for ECD*/
qui replace ecd_wtp_baseline=1 if s6q12==1 & s6q08==1;
qui replace ecd_wtp_baseline=0 if s6q12==2 & s6q08==1;
qui replace ecd_wtpq_baseline=s6q13 if s6q08==1;
qui gen ecd_wtppct_baseline=ecd_wtpq_baseline*12/expend_yr_hhpc;
lab var ecd_wtp_baseline "Willing to pay for ECD at baseline";
lab var ecd_wtpq_baseline "Amount willing to pay for ECD at baseline (GMD)";
lab var ecd_wtppct_baseline "Amount willing to pay for ECD as shr of monthly HH pc expenditure at baseline";

* Module 7: mother's birth history;
* Module 8: mother's antenatal care;

* Module 9: child health status;
foreach x in bednet sick {;
	qui gen child_`x'_baseline=.;
};
qui replace child_bednet_baseline=1 if s9q02==1;
qui replace child_bednet_baseline=0 if s9q02==2;
qui replace child_sick_baseline=1 if s9q03==1;
qui replace child_sick_baseline=0 if s9q03==2;
lab var child_bednet_baseline "child slept under bednet yesterday, baseline";
lab var child_sick_baseline "child sick in last 3 days, baseline";

* Module 10: vaccinations (disregard polio booster because starts at 4 years);
foreach x in deworm bcg hepb polio0 polio1 polio2 polio3 polio4 pent1 pent2 pent3 dpt pneumo1 pneumo2 pneumo3 measles yf {;
	qui gen `x'_baseline=.;
	lab var `x'_baseline "Had received `x' vaccine or treatment at baseline";
};
forval i=1/10 {;
	qui gen vitA`i'_baseline=.;
};
/*vaccines*/
qui replace deworm_baseline=1 if s10q03==1 & selected_child_code==s10q02;
qui replace deworm_baseline=0 if s10q03==2 & selected_child_code==s10q02;
qui replace bcg_baseline=1 if s10q05a__bcg_year>=2003 & s10q05a__bcg_year<2012 & s10q05a__bcg_year!=. & selected_child_code==s10q02;
qui replace bcg_baseline=0 if s10q05a__bcg_year>2012 & selected_child_code==s10q02;
qui replace hepb_baseline=1 if s10q05b_year>=2003 & s10q05b_year<2012 & s10q05b_year!=. & selected_child_code==s10q02;
qui replace hepb_baseline=0 if s10q05b_year>2012 & selected_child_code==s10q02;
qui replace polio0_baseline=1 if s10q05c_polio0_year>=2003 & s10q05c_polio0_year<2012 & s10q05c_polio0_year!=. & selected_child_code==s10q02;
qui replace polio0_baseline=0 if s10q05c_polio0_year>2012 & selected_child_code==s10q02;
qui replace polio1_baseline=1 if s10q05d__polio1_year>=2003 & s10q05d__polio1_year<2012 & s10q05d__polio1_year!=. & selected_child_code==s10q02;
qui replace polio1_baseline=0 if s10q05d__polio1_year>2012 & selected_child_code==s10q02;
qui replace polio2_baseline=1 if s10q05e__polio2_year>=2003 & s10q05e__polio2_year<2012 & s10q05e__polio2_year!=. & selected_child_code==s10q02;
qui replace polio2_baseline=0 if s10q05e__polio2_year>2012 & selected_child_code==s10q02;
qui replace polio3_baseline=1 if s10q05f__polio4__year>=2003 & s10q05f__polio4__year<2012 & s10q05f__polio4__year!=. & selected_child_code==s10q02;
qui replace polio3_baseline=0 if s10q05f__polio4__year>2012 & selected_child_code==s10q02;
qui replace polio4_baseline=1 if s10q05g__polio4_year>=2003 & s10q05g__polio4_year<2012 & s10q05g__polio4_year!=. & selected_child_code==s10q02;
qui replace polio4_baseline=0 if s10q05g__polio4_year>2012 & selected_child_code==s10q02;
qui replace pent1_baseline=1 if s10q05i__pentavalent1__year>=2003 & s10q05i__pentavalent1__year<2012 & s10q05i__pentavalent1__year!=. & selected_child_code==s10q02;
qui replace pent1_baseline=0 if s10q05i__pentavalent1__year>2012 & selected_child_code==s10q02;
qui replace pent2_baseline=1 if s10q05j__pentavalent2__year>=2003 & s10q05j__pentavalent2__year<2012 & s10q05j__pentavalent2__year!=. & selected_child_code==s10q02;
qui replace pent2_baseline=0 if s10q05j__pentavalent2__year>2012 & selected_child_code==s10q02;
qui replace pent3_baseline=1 if s10q05k__pentavalent3__year>=2003 & s10q05k__pentavalent3__year<2012 & s10q05k__pentavalent3__year!=. & selected_child_code==s10q02;
qui replace pent3_baseline=0 if s10q05k__pentavalent3__year>2012 & selected_child_code==s10q02;
qui replace dpt_baseline=1 if s10q05l__dptbooster__year>=2003 & s10q05l__dptbooster__year<2012 & s10q05l__dptbooster__year!=. & selected_child_code==s10q02;
qui replace dpt_baseline=0 if s10q05l__dptbooster__year>2012 & selected_child_code==s10q02;
qui replace pneumo1_baseline=1 if s10q05m__pneumo__year>=2003 & s10q05m__pneumo__year<2012 & s10q05m__pneumo__year!=. & selected_child_code==s10q02;
qui replace pneumo1_baseline=0 if s10q05m__pneumo__year>2012 & selected_child_code==s10q02;
qui replace pneumo2_baseline=1 if s10q05n__pneumo__year>=2003 & s10q05n__pneumo__year<2012 & s10q05n__pneumo__year!=. & selected_child_code==s10q02;
qui replace pneumo2_baseline=0 if s10q05n__pneumo__year>2012 & selected_child_code==s10q02;
qui replace pneumo3_baseline=1 if s10q05o__pneumo3__year>=2003 & s10q05o__pneumo3__year<2012 & s10q05o__pneumo3__year!=. & selected_child_code==s10q02;
qui replace pneumo3_baseline=0 if s10q05o__pneumo3__year>2012 & selected_child_code==s10q02;
qui replace measles_baseline=1 if s10q05p__measles__year>=2003 & s10q05p__measles__year<2012 & s10q05p__measles__year!=. & selected_child_code==s10q02;
qui replace measles_baseline=0 if s10q05p__measles__year>2012 & selected_child_code==s10q02;
qui replace yf_baseline=1 if s10q05q>=2003 & s10q05q<2012 & s10q05q!=. & selected_child_code==s10q02;
qui replace yf_baseline=0 if s10q05q>2012 & selected_child_code==s10q02;
local vaccines "deworm bcg hepb polio0 polio1 polio2 polio3 polio4 pent1 pent2 pent3 dpt pneumo1 pneumo2 pneumo3 
	measles yf";
foreach x in `vaccines' {;
	ren `x'_baseline `x'; /*temporarily rename for easier aggregation*/
};
qui egen vaccinecount_baseline=rowtotal(`vaccines');
qui gen vaccinepct_baseline=vaccinecount_baseline/17;
qui gen vaccineany_baseline=(vaccinecount_baseline>0);
lab var vaccinecount_baseline "# of vaccines (of 17) received at baseline";
lab var vaccinepct_baseline "% of vaccines (of 17) received at baseline (zeroes could mean missing)";
lab var vaccineany_baseline "any vaccine or treatment received at baseline";
foreach x in `vaccines' {;
	ren `x' `x'_baseline; /*temporarily rename for easier aggregation*/
};

/*Vitamin A supplemenation*/
qui replace vitA1_baseline=1 if s10q06a__1stdose__year>=2003 & s10q06a__1stdose__year<2012 & s10q06a__1stdose__year!=. & selected_child_code==s10q02;
qui replace vitA1_baseline=0 if s10q06a__1stdose__year>2012 & selected_child_code==s10q02;
qui replace vitA2_baseline=1 if s10q06b__2nddose__year>=2003 & s10q06b__2nddose__year<2012 & s10q06b__2nddose__year!=. & selected_child_code==s10q02;
qui replace vitA2_baseline=0 if s10q06b__2nddose__year>2012 & selected_child_code==s10q02;
qui replace vitA3_baseline=1 if s10q06c__3rddose__year>=2003 & s10q06c__3rddose__year<2012 & s10q06c__3rddose__year!=. & selected_child_code==s10q02;
qui replace vitA3_baseline=0 if s10q06c__3rddose__year>2012 & selected_child_code==s10q02;
qui replace vitA4_baseline=1 if s10q06d__4thdose_year>=2003 & s10q06d__4thdose_year<2012 & s10q06d__4thdose_year!=. & selected_child_code==s10q02;
qui replace vitA4_baseline=0 if s10q06d__4thdose_year>2012 & selected_child_code==s10q02;
qui replace vitA5_baseline=1 if s10q06e__5thdose__year>=2003 & s10q06e__5thdose__year<2012 & s10q06e__5thdose__year!=. & selected_child_code==s10q02;
qui replace vitA5_baseline=0 if s10q06e__5thdose__year>2012 & selected_child_code==s10q02;
qui replace vitA6_baseline=1 if s10q06f__6thedose__year>=2003 & s10q06f__6thedose__year<2012 & s10q06f__6thedose__year!=. & selected_child_code==s10q02;
qui replace vitA6_baseline=0 if s10q06f__6thedose__year>2012 & selected_child_code==s10q02;
qui replace vitA7_baseline=1 if s10q06g__7thdose__year>=2003 & s10q06g__7thdose__year<2012 & s10q06g__7thdose__year!=. & selected_child_code==s10q02;
qui replace vitA7_baseline=0 if s10q06g__7thdose__year>2012 & selected_child_code==s10q02;
qui replace vitA8_baseline=1 if s10q06h__8thedose_year>=2003 & s10q06h__8thedose_year<2012 & s10q06h__8thedose_year!=. & selected_child_code==s10q02;
qui replace vitA8_baseline=0 if s10q06h__8thedose_year>2012 & selected_child_code==s10q02;
qui replace vitA9_baseline=1 if s10q06i__9th_dose_year>=2003 & s10q06i__9th_dose_year<2012 & s10q06i__9th_dose_year!=. & selected_child_code==s10q02;
qui replace vitA9_baseline=0 if s10q06i__9th_dose_year>2012 & selected_child_code==s10q02;
qui replace vitA10_baseline=1 if s10q06j__10th_dose_year>=2003 & s10q06j__10th_dose_year<2012 & s10q06j__10th_dose_year!=. & selected_child_code==s10q02;
qui replace vitA10_baseline=0 if s10q06j__10th_dose_year>2012 & selected_child_code==s10q02;
order vitA*_baseline;
qui egen vitAcount_baseline=rowtotal(vitA1_baseline-vitA10_baseline);
qui gen vitApct_baseline=vitAcount_baseline/10;
qui gen vitAany_baseline=(vitAcount_baseline>0);
lab var vitAcount_baseline "# of vitamin A supplements (of 10) received at baseline";
lab var vitApct_baseline "% of vitamin A supplements (of 10) received at baseline (zeroes could mean missing)";
lab var vitAany_baseline "any vitamin A supplements received at baseline";

* Module 11: monthly weight monitoring of child;
* Module 12: mother Knowledge, Attitude, and Practice (KAP);
* Module 13: BFCI services in village;

* Module 14: home environment;
/*books in household*/
foreach x in none 1to2 3to5 6plus {;
	qui gen booksinhh_`x'_bl=.;
	lab var booksinhh_`x'_bl "books in household at baseline: `x'";
};
qui replace booksinhh_none_bl=1 if s14q02==1;
qui replace booksinhh_none_bl=0 if s14q02!=1 & s14q02!=.;
qui replace booksinhh_1to2_bl=1 if s14q02==2;
qui replace booksinhh_1to2_bl=0 if s14q02!=2 & s14q02!=.;
qui replace booksinhh_3to5_bl=1 if s14q02==3;
qui replace booksinhh_3to5_bl=0 if s14q02!=3 & s14q02!=.;
qui replace booksinhh_6plus_bl=1 if s14q02==4|s14q02==5;
qui replace booksinhh_6plus_bl=0 if s14q02!=4 & s14q02!=5 & s14q02!=.;

/*stimulating objects (toys, etc.) and play (stories, singing, etc.)*/
order s14q04* s14q05*;
qui egen stimobjectscount_bl=anycount(s14q04_1-s14q04_10), v(1);
qui gen stimobjectspct_bl=stimobjectscount/10;
qui egen stimplaycount_bl=anycount(s14q05_1-s14q05_6), v(1);
qui gen stimplaypct_bl=stimplaycount/6;
qui gen stimindex_bl=(stimobjectscount_bl+stimplaycount_bl)/16;
foreach x in objects play {;
	lab var stim`x'count_bl "# of stimulating `x' listed at baseline";
	lab var stim`x'count_bl "% of stimulating `x' items listed at baseline";
};
lab var stimindex_bl "stimulation index (% of play activities & objects, of 16), baseline";

/*disciplinary actions*/
qui gen disc_nonvlnt_baseline=(s14q06>=1 & s14q06<=5);
qui gen disc_psychagg_baseline=(s14q06>=6 & s14q06<=10);
qui gen disc_vlnt_baseline=(s14q06>10);
qui gen discsevere_nonvlnt_baseline=(s14q07>=1 & s14q07<=5);
qui gen discsevere_psychagg_baseline=(s14q07>=6 & s14q07<=10);
qui gen discsevere_vlnt_baseline=(s14q07>10);
foreach x in nonvlnt psychagg vlnt {;
	qui replace disc_`x'_baseline=. if s14q06==.;
	qui replace discsevere_`x'_baseline=. if s14q07==.;
};
lab var disc_nonvlnt_baseline "usual discipline for bad behavior (baseline): non-violent";
lab var disc_psychagg_baseline "usual discipline for bad behavior (baseline): psychological aggression";
lab var disc_vlnt_baseline "usual discipline for bad behavior (baseline): violent";
lab var discsevere_nonvlnt_baseline "usual discipline for very bad behavior (baseline): non-violent";
lab var discsevere_psychagg_baseline "usual discipline for very bad behavior (baseline): psychological aggression";
lab var discsevere_vlnt_baseline "usual discipline for very bad behavior (baseline): violent";

/*time with child*/
qui gen incare_othchild_none_bl=(s14q08==1);
qui gen incare_othchild_some_bl=(s14q08==2);
qui gen incare_othchild_occas_bl=(s14q08==3);
qui gen incare_othchild_most_bl=(s14q08==4);
qui gen incare_alone_none_bl=(s14q09==1);
qui gen incare_alone_some_bl=(s14q09==2);
qui gen incare_alone_occas_bl=(s14q09==3);
qui gen incare_alone_most_bl=(s14q09==4);
foreach x in none some occas most {;
	qui replace incare_othchild_`x'_bl=. if s14q08<1|s14q08>4;
	qui replace incare_alone_`x'_bl=. if s14q09<1|s14q09>4;
};
qui gen funtime_never_bl=(s14q10==1);
qui gen funtime_some_bl=(s14q10==2);
qui gen funtime_often_bl=(s14q10==3);
qui gen funtime_always_bl=(s14q10==4);
foreach x in never some often always {;
	qui replace funtime_`x'_bl=. if s14q10<1|s14q10>4;
};
lab var incare_othchild_none_bl "Left in care of child<10 yrs, none (baseline)";
lab var incare_othchild_some_bl "Left in care of child<10 yrs, 1-2days/wk (baseline)";
lab var incare_othchild_occas_bl "Left in care of child<10 yrs, 3-4days/wk (baseline)";
lab var incare_othchild_most_bl "Left in care of child<10 yrs, 5-7days/wk (baseline)";
lab var incare_alone_none_bl "Left alone, none (baseline)";
lab var incare_alone_some_bl "Left alone, 1-2days/wk (baseline)";
lab var incare_alone_occas_bl "Left alone, 3-4days/wk (baseline)";
lab var incare_alone_most_bl "Left alone, 5-7days/wk (baseline)";
lab var funtime_never_bl "Spend time with child for fun, <30min/day (baseline)";
lab var funtime_some_bl "Spend time with child for fun, <30-60min/day (baseline)";
lab var funtime_often_bl "Spend time with child for fun, <1-2hr/day (baseline)";
lab var funtime_always_bl "Spend time with child for fun, >2hr/day (baseline)";

* Module 15: mother mental health;
* follow code from ecd_gambia_moussa_old.do (line 922-934);
*****Mother health;
qui recode s15q04 (0=3 "Most of the time")(1=2 "Occasionally or a moderate amount of th")
	(2=1 "Some or a little of the time")(3=0 "Rarely or none of the time"), gen(s15q04_r);
qui recode s15q07 (0=3 "Most of the time")(1=2 "Occasionally or a moderate amount of th")
	(2=1 "Some or a little of the time")(3=0 "Rarely or none of the time"), gen(s15q07_r);
alpha s15q01- s15q03 s15q04_r s15q05 s15q06 s15q07_r s15q08 s15q09 s15q10 s15q11, asis i;
qui egen mother_health1_baseline= rowmean(s15q01 s15q02 s15q03 s15q04_r s15q05 s15q06 s15q07_r s15q08 s15q09 s15q10 s15q11);
lab var mother_health1_baseline "mother mental health index (0-3 scale, lower is healthier, baseline)";

* version 2: code from ecd_gambia_moussa_old.do, line 983-999;
foreach var in s15q01 s15q02 s15q03 s15q05 s15q06 s15q08 s15q09 s15q10 s15q11 {;
	qui recode `var' (0 = 1 )(1 2 3 =0),gen(`var'r);
};

foreach var in s15q04 s15q07 {;
	qui recode `var' (3 = 1 )(0 1 2 =0)(else=.),gen(`var'r);
};
qui egen mother_health2 = rowmean(s15q01r s15q02r s15q03r s15q05r s15q06r s15q08r s15q09r s15q10r s15q11r s15q04r s15q07r);
qui gen mother_health_baseline= 100*mother_health2;
pca s15q01r s15q02r s15q03r s15q05r s15q06r s15q08r s15q09r s15q10r s15q11r s15q04r s15q07r;
predict p1;
corr p1 mother_health_baseline;
drop p1;
lab var mother_health_baseline "mother mental health index (% of items 'most of the time', which is bad, baseline)";
drop mother_health2;

* Baseline skills tests;
* follow code from gambia_ecd_mdatcheck1.do;
/*MDAT FINE MOTOR*/
ren fm36237 fm36q37;
aorder fm36q??;
qui egen finemotor_baseline_miss=rowmiss(fm36q18-fm36q41);
qui egen finemotor_baseline=rowtotal(fm36q18-fm36q41), missing;
tab finemotor_baseline_miss, mi;
qui replace finemotor_baseline=. if finemotor_baseline_miss>=1; /*use complete cases only*/
qui su finemotor_baseline;
qui gen zfinemotor_baseline=(finemotor_baseline-r(mean))/r(sd);
lab var finemotor_baseline_miss "MDAT fine motor baseline, # of missing items (of 23)";
lab var finemotor_baseline "MDAT fine motor baseline (of 23)";
lab var zfinemotor_baseline "MDAT fine motor baseline z-score";

/*version based only on common items between baseline & endline MDAT (see "MDAT crosswalk.xlsx")*/
local fm_common "fm36q27-fm36q41"; /*14 common items (#31 missing)*/
qui egen finemotor_comm_baseline=rowtotal(`fm_common'), missing;
qui replace finemotor_comm_baseline=. if finemotor_baseline_miss>=1; /*use complete cases only*/
qui su finemotor_comm_baseline;
qui gen zfinemotor_comm_baseline=(finemotor_comm_baseline-r(mean))/r(sd);
lab var finemotor_comm_baseline "MDAT fine motor baseline, common baseline/endline items only (of 14)";
lab var zfinemotor_comm_baseline "MDAT fine motor baseline z-score, common baseline/endline";

/*MDAT LANGUAGE & HEARING*/
aorder lh36q??;
qui egen langhear_baseline_miss=rowmiss(lh36q17-lh36q43);
qui egen langhear_baseline=rowtotal(lh36q17-lh36q43), missing;
tab langhear_baseline_miss, mi;
qui replace langhear_baseline=. if langhear_baseline_miss>=1; /*use complete cases only*/
qui su langhear_baseline;
qui gen zlanghear_baseline=(langhear_baseline-r(mean))/r(sd);
lab var langhear_baseline_miss "MDAT language & hearing baseline, # of missing items (of 26)";
lab var langhear_baseline "MDAT language & hearing baseline (of 26)";
lab var zlanghear_baseline "MDAT language & hearing baseline z-score";

/*version based only on common items between baseline & endline MDAT (see "MDAT crosswalk.xlsx")*/
local lh_common "lh36q17 lh36q20 lh36q31-lh36q43"; /*15 common items*/
qui egen langhear_comm_baseline=rowtotal(`lh_common'), missing;
qui replace langhear_comm_baseline=. if langhear_baseline_miss>=1; /*use complete cases only*/
qui su langhear_comm_baseline;
qui gen zlanghear_comm_baseline=(langhear_comm_baseline-r(mean))/r(sd);
lab var langhear_comm_baseline "MDAT language & hearing baseline, common baseline/endline items only (of 15)";
lab var zlanghear_comm_baseline "MDAT language & hearing baseline z-score, common baseline/endline";

/*adjust MDAT scores for age and gender*/
qui gen child_age_yrs_dob2=child_age_yrs_dob^2;
foreach x in finemotor langhear finemotor_comm langhear_comm {;
	qui xi: reg `x'_baseline child_age_yrs_dob child_age_yrs_dob2 child_female; /*doesn't matter if Y=raw or z score*/
	qui predict e, residuals; 
	qui egen z`x'_adj_baseline=std(e);
	drop e;
};
lab var zfinemotor_adj_baseline "MDAT fine motor baseline z-score (age-adjusted)";
lab var zlanghear_adj_baseline "MDAT language & hearing baseline z-score (age-adjusted)";
lab var zfinemotor_comm_adj_baseline "MDAT fine motor baseline z-score (age-adjusted), common baseline/endline items only";
lab var zlanghear_comm_adj_baseline "MDAT language & hearing baseline z-score (age-adjusted), common baseline/endline items only";

/*adjust MDAT scores by age, with two differences from above:
	1) non-parametric adjustment
	2) adjust both mean and variance by age
	--code follows IRT normalization in gambia_ecd_clean3.do, which follows Pam Jervis analysis for Gambia ECD 0-3 
		data. For example, see ecd_gambia_ses.do, lines 415-436.*/
foreach x in finemotor langhear {;
	/*variance conditional on age*/
	qui lpoly `x'_baseline child_age_yrs_dob, at(child_age_yrs_dob) gen(mu) nograph;
	qui gen r=`x'_baseline-mu; /*residual and its square*/
	qui gen r2=r^2;
	qui lpoly r2 child_age_yrs_dob, at(child_age_yrs_dob) gen(var) nograph;
	qui gen std = sqrt(var);

	/*z-score adjusted for age*/
	if ("`x'"=="finemotor") local xx "fm";
	if ("`x'"=="langhear") local xx "lh";
	qui gen z`xx'_adj_baseline=(`x'_baseline-mu)/std;
	drop mu r r2 std var;
};
lab var zfm_adj_baseline "MDAT fine motor baseline z-score (age-adjusted, both mean & sd)";
lab var zlh_adj_baseline "MDAT language & hearing baseline z-score (age-adjusted, both mean & sd)";

/*Subsets of MDAT items (36-59 months) that correspond to experimental curriculum (GOALS)*/
/*See "Curriculum vs. MDAT Matrix.xlsx" for details*/
/*fine motor skills groups*/
/*note that order items are common between baseline and endline. Create common version of blocks and draw items below also.*/
qui egen finemotor_blocks_baseline=rowtotal(fm36q20 fm36q22 fm36q23 fm36q30 fm36q35 fm36q36);
qui gen finemotor_blocks_pct_baseline=finemotor_blocks_baseline/6;
qui egen finemotor_blk_comm_baseline=rowtotal(fm36q30 fm36q35 fm36q36);
qui gen finemotor_blk_comm_pct_baseline=finemotor_blk_comm_baseline/3;
qui egen finemotor_draw_baseline=rowtotal(fm36q18 fm36q19 fm36q27 fm36q32-fm36q34);
qui gen finemotor_draw_pct_baseline=finemotor_draw_baseline/6;
qui egen finemotor_draw_comm_baseline=rowtotal(fm36q27 fm36q32-fm36q34);
qui gen finemotor_draw_comm_pct_baseline=finemotor_draw_comm_baseline/4;
qui egen finemotor_order_baseline=rowtotal(fm36q37 fm36q38);
qui gen finemotor_order_pct_baseline=finemotor_order_baseline/2;

/*language and hearing skills groups*/
/*note that all items are common between baseline and endline*/
qui egen langhear_name_baseline=rowtotal(lh36q20 lh36q41-lh36q43);
qui gen langhear_name_pct_baseline=langhear_name_baseline/4;
qui gen langhear_sentence_baseline=lh36q17;
qui egen langhear_count_baseline=rowtotal(lh36q33-lh36q35);
qui gen langhear_count_pct_baseline=langhear_count_baseline/3;
qui egen langhear_colors_baseline=rowtotal(lh36q37-lh36q40);
qui gen langhear_colors_pct_baseline=langhear_colors_baseline/4;

lab var finemotor_blocks_baseline "MDAT fine motor skills (baseline) play with blocks (of 6)";
lab var finemotor_blocks_pct_baseline "MDAT fine motor skills (baseline) play with blocks (% of 6)";
lab var finemotor_blk_comm_baseline "MDAT fine motor skills (baseline) play with blocks (of 3), common baseline/endline items";
lab var finemotor_blk_comm_pct_baseline "MDAT fine motor skills (baseline) play with blocks (% of 3), common baseline/endline items";
lab var finemotor_draw_baseline "MDAT fine motor skills (baseline) draw lines & shapes (of 6)";
lab var finemotor_draw_pct_baseline "MDAT fine motor skills (baseline) draw lines & shapes (% of 6)";
lab var finemotor_draw_comm_baseline "MDAT fine motor skills (baseline) draw lines & shapes (of 4), common baseline/endline items";
lab var finemotor_draw_comm_pct_baseline "MDAT fine motor skills (baseline) draw lines & shapes (% of 4), common baseline/endline items";
lab var finemotor_order_baseline "MDAT fine motor skills (baseline) order rows of items (of 2)";
lab var finemotor_order_pct_baseline "MDAT fine motor skills (baseline) order rows of items (% of 2)";
lab var langhear_name_baseline "MDAT language skills (baseline) knows own name & its letters";
lab var langhear_name_pct_baseline "MDAT language skills (baseline) knows own name & its letters (% of 4)";
lab var langhear_sentence_baseline "MDAT language skills (baseline) speaks in clear sentences";
lab var langhear_count_baseline "MDAT language skills (baseline) counting (of 3)";
lab var langhear_count_pct_baseline "MDAT language skills (baseline) counting (% of 3)";
lab var langhear_colors_baseline "MDAT language skills (baseline) name colors (of 4)";
lab var langhear_colors_pct_baseline "MDAT language skills (baseline) name colors (% of 4)";

* ANTHROPOMETRICS;
* appear to be taken at endline and not baseline, but q1600 series appears to be endline and shows growth
	compared to am36 series -- NEED TO CHECK;
* follow code from ecd_gambia_moussa_old.do;
qui egen child_height = rowmean(am36q03_1 am36q03_2 am36q03_3); //height  
qui egen child_weight = rowmean(am36q04_1 am36q04_2 am36q04_3); // weight 
qui egen child_armcirm = rowmean(am36q05_1 am36q05_2 am36q05_3); // brachial cirmcum
qui gen childBMI = child_weight*10000/(child_height)^2;
lab var child_height "height (cm)";
lab var child_weight "weight (kg)";
lab var child_armcirm "brachial circumference (cm), baseline";
foreach x in height weight {;
	lab var child_`x' "child `x', baseline";
};	
lab var childBMI "child body mass index, baseline";

qui gen agemonths_approx=floor(child_age_mths_dob);
run `do'\gambia_ecd_clean1_aux_baseline;	
drop agemonths_approx;

/*get height/weight for age z-scores (WHO 2006): age 5 and under*/
/*note that age in months only valid for children 5 and under*/
/*cap z-scores at +/-6*/
zscore06, a(child_age_mths) s(child_sex) h(child_height) w(child_weight);
foreach x in haz waz whz bmiz {;
	ren `x'06 `x'_baseline;
};
foreach x in haz waz whz {;
	qui replace `x'_baseline=. if child_age_mths_impute==1|child_age_mths>60|`x'_baseline==99;
};
qui replace bmiz_baseline=. if haz_baseline==.|waz_baseline==.;
foreach x in haz waz whz bmiz {;
	qui replace `x'_baseline=-6 if `x'_baseline<-6 & `x'_baseline!=.;
	qui replace `x'_baseline=6 if `x'_baseline>6 & `x'_baseline!=.;
};

/*use age based on DOB for alternate measure*/
zscore06, a(child_age_mths_dob) s(child_sex) h(child_height) w(child_weight);
foreach x in haz waz whz bmiz {;
	ren `x'06 `x'_dob_baseline;
};
foreach x in haz waz whz {;
	qui replace `x'_dob_baseline=. if child_age_mths_dob>60|`x'_baseline==99;
};
qui replace bmiz_dob_baseline=. if haz_dob_baseline==.|waz_dob_baseline==.;

/*get height for age z-scores (WHO 2006): over age 5*/
/*based on WHO 2007, downloaded from http://www.who.int/growthref/who2007_height_for_age/en/,
	http://www.who.int/growthref/hfa_boys_z_WHO2007_exp.txt,
	http://www.who.int/growthref/hfa_girls_z_WHO2007_exp.txt*/
qui gen agemonths_approx=floor(child_age_mths_dob);
qui replace agemonths_approx=61 if child_age_mths_dob>60 & child_age_mths_dob<61 & child_age_mths_dob!=.;
qui save `cleandata'\ecdtemp, replace;
qui insheet using `baseline'\hfa_girls_z_WHO2007_exp.txt, tab clear;
qui gen child_sex=2;
ren month agemonths_approx;
keep child_sex agemonths_approx agemonths_approx m stdev;
qui save `cleandata'\haz_girls, replace;
qui insheet using `baseline'\hfa_boys_z_WHO2007_exp.txt, tab clear;
qui gen child_sex=1;
ren month agemonths_approx;
keep child_sex agemonths_approx m stdev;
qui append using `cleandata'\haz_girls;
qui gen selected_child_gender=child_sex;
qui gen child_gender_modified=child_sex;
qui save `cleandata'\haztemp, replace;
qui use `cleandata'\ecdtemp, clear;
drop _merge;
qui merge m:1 child_gender_modified agemonths_approx using `cleandata'\haztemp;
drop _merge;
qui replace haz_dob_baseline=(child_height - m)/stdev if agemonths_approx>=61;
foreach x in haz waz whz bmiz {;
	qui replace `x'_dob_baseline=-6 if `x'_dob_baseline<-6 & `x'_dob_baseline!=.;
	qui replace `x'_dob_baseline=6 if `x'_dob_baseline>6 & `x'_dob_baseline!=.;
};
drop agemonths_approx m stdev;

* CLEAN ENDLINE DATA;
* Cover sheet: HH characteristics;
qui egen numchildrenhh_endline=rowtotal(number_of_under_7years_compound number_of_7_13_years_in_the_comp); /*note that this asks about "compound," and some numbers very high*/
qui replace numchildrenhh_endline=numchildrenhh_endline-1; /*exclude selected child*/
qui gen mother_head_endline=(q501==1);
lab var numchildrenhh_endline "number of children under 14 in compound (excl. selected child), endline";
lab var mother_head_endline "mother is head of household, endline";

* Module 2: employment;
* head of HH, mother (or main caregiver) labor;
qui gen mother_endline=(q200_2==2|q200_1==1 & q501==1);
foreach y in headhh mother {;
	foreach x in hrswrk sector {;
		qui gen `y'_`x'_el=.;
	};
};
qui replace headhh_hrswrk_el=q201_1 if q200_1==1;
qui replace headhh_sector_el=q210_1 if q200_1==1;
qui replace mother_hrswrk_el=q201_1 if q200_2==2|q200_2==3|(q200_1==1 & q501==1);
qui replace mother_sector_el=q210_1 if q200_2==2|q200_2==3|(q200_1==1 & q501==1);
foreach y in headhh mother {;
	qui gen `y'_hrswrk_cat_el=.;
	qui replace `y'_hrswrk_cat_el=1 if `y'_hrswrk_el==0;
	qui replace `y'_hrswrk_cat_el=2 if `y'_hrswrk_el>0 & `y'_hrswrk_el<=20 & `y'_hrswrk_el!=.;
	qui replace `y'_hrswrk_cat_el=3 if `y'_hrswrk_el>20 & `y'_hrswrk_el<=40 & `y'_hrswrk_el!=.;
	qui replace `y'_hrswrk_cat_el=4 if `y'_hrswrk_el>40 & `y'_hrswrk_el<=60 & `y'_hrswrk_el!=.;
	qui replace `y'_hrswrk_cat_el=5 if `y'_hrswrk_el>=60 & `y'_hrswrk_el!=.;
};
qui gen headhh_work_el=(headhh_hrswrk_el>0 & headhh_hrswrk_el!=. & q200_1==1);
qui replace headhh_work_el=. if headhh_hrswrk_el==.;
qui gen headhh_ag_el=(headhh_sector_el==1);
qui replace headhh_ag_el=. if q200_1==.|q200_1!=1;
qui gen mother_work_el=(mother_hrswrk_el>0 & mother_hrswrk_el!=. & (q200_2==2|q200_2==3|(q200_1==1 & q501==1)));
qui replace mother_work_el=. if mother_hrswrk_el==.;
qui gen mother_ag_el=(mother_sector_el==1);
qui replace mother_ag_el=. if q200_2==.|q200_2==1;
lab var mother_endline "respondent to mother endline labor questions is mother, not different caregiver";
foreach y in headhh mother {;
	lab val `y'_hrswrk_cat_el headhh_hrswrk_cat;
	lab val `y'_sector_el s3q10_1;
	lab var `y'_hrswrk_el "hours worked for pay last week, `y' (endline)";
	lab var `y'_hrswrk_cat_el "hours worked for pay last week (categorical), `y' (endline)";
	lab var `y'_work_el "`y' worked for pay last week (endline)";
	lab var `y'_sector_el "sector of work, `y' (endline)";
	lab var `y'_ag_el "`y' works in agriculture (endline)";	
};	
qui gen mother_unpaid_el=.;
qui replace mother_unpaid_el=1 if (q202_2==1|q202_2==2) & (q200_2==2|q200_2==3|(q200_1==1 & q501==1));
qui replace mother_unpaid_el=0 if q202_2==3 & (q200_2==2|q200_2==3|(q200_1==1 & q501==1));
lab var mother_unpaid_el "mother does unpaid work (endline)";

* Module 3: expenditures;
* follow code from ecd_gambia_moussa_old.do;
qui egen expend_daily_el = rowtotal(q302_101 q302_102 q302_103 q302_104 q302_105 q302_106 q302_107 q302_108 q302_109 
	q302_110 q302_111 q302_112 q302_113 q302_114 q302_115 q302_116 q302_117 q302_118 q302_119 q302_120 q302_121
	q302_122 q302_123 q302_124);
qui egen expend_monthly_el = rowtotal(q304_201 q304_202 q304_203 q304_204 q304_205 q304_206 q304_207 q304_208 q304_209 q304_210);
qui egen expend_yearly_el = rowtotal(q304_211 q304_212 q304_213 q304_214 q304_215 q304_216 q304_217 q304_218 q304_219 q304_220);
qui gen expend_yr_hhpc_el = (12*(30*expend_daily_el + expend_monthly_el) + expend_yearly_el)/number_of_people_in_this_househo;
qui gen expend_yr_hhpc_usd_el=expend_yr_hhpc_el/32.6333; /*GMD/USD exchange rate in October 2013, http://www.exchangerates.org.uk/USD-GMD-31_10_2013-exchange-rate-history.html*/
qui gen anyexpend_ecd_endline=(q303_211==1);
qui gen expend_ecd_endline=q304_211;
qui replace expend_ecd_endline=0 if q303_211!=1;
qui gen expend_ecd_usd_endline=expend_ecd_endline/32.6333;
foreach x in daily monthly yearly {;
	lab var expend_`x'_el "`x' HH expenditure (GMD, endline)";
};
lab var expend_yr_hhpc_el "annual household expenditure per capita (GMD, endline)";
lab var expend_yr_hhpc_usd_el "annual household expenditure per capita (USD, endline)";
lab var anyexpend_ecd_endline "any expenditure on ECD, endline";
lab var expend_ecd_endline "annual household expenditure on ECD (GMD, endline)";
lab var expend_ecd_usd_endline "annual household expenditure on ECD (USD, endline)";

* Module 5: child health;
foreach x in bednet sick {;
	qui gen child_`x'_endline=.;
};
qui replace child_bednet_endline=1 if q502==1;
qui replace child_bednet_endline=0 if q502==2;
qui replace child_sick_endline=1 if q503==1;
qui replace child_sick_endline=0 if q503==2;
lab var child_bednet_endline "child slept under bednet yesterday, endline";
lab var child_sick_endline "child sick in last 3 days, endline";

* Module 6: mother knowledge;
* follow code from ecd_endline_gambia_analysis_012014_3-6.do (lines 833-928);
* each item is from 1-3, with 1 highest knowledge and 3 lowest (1=very important, 2=somewhat important, 3=not important);
aorder q6*;

*****Mother knowlegde on hygiene and sanitation;
**food hygiene;
alpha q601_1- q602_7, asis i;
qui egen MK_food_hygiene = rowmean(q601_1- q602_7);

**hand washing;
alpha q603_1- q604_3, asis i;
qui egen MK_hands_washing = rowmean(q603_1- q604_3);

**waste management;
alpha q605_1- q605_4, asis i;
qui egen MK_waste_management = rowmean(q605_1- q605_4);

**drinking water management;
qui recode q606_5 (1=3 "Not important")(2=2 "Somewhat important")(3=1 "Very important"), gen(q606_5_r);
alpha q606_1- q606_4 q606_5_r, asis i;
qui egen MK_drinking_water = rowmean(q606_1- q606_4 q606_5_r);

***hygiene and sanitation in general;
alpha q601_1- q606_4 q606_5_r, asis i;
qui egen MK_hygiene_sanitation = rowmean(q601_1- q606_4 q606_5_r);

*****Mother knowlegde on health;
**danger sign;
alpha q608_1- q608_9, asis i;
qui egen MK_danger_sign = rowmean(q608_1- q608_9);

**deworming;
alpha q610_1- q610_4, asis i;
qui egen MK_deworming = rowmean(q610_1- q610_4);

***health issues (deworming and danger sign); 
alpha q608_1- q608_9 q610_1- q610_4, asis i;
qui egen MK_health = rowmean(q608_1- q608_9 q610_1- q610_4);

***health issues in general;
alpha q607_1 - q610_4, asis i;
qui egen MK_health_g = rowmean(q607_1 - q610_4);

*****Mother knowlegde on nutrition;
**vitamin A advantages;
alpha q613_1- q613_4, asis i;
qui egen MK_vitamin_A = rowmean(q613_1- q613_4);

**Iodized salt advantages;
alpha q614_1- q614_5, asis i;
qui egen MK_iodized_salt = rowmean(q614_1- q614_5);

**Suppementary food;
alpha q615_1- q616_6, asis i;
qui egen MK_supplementary_food = rowmean(q615_1- q616_6);

***nutrition in general;
alpha q611_1- q616_6, asis i;
qui egen MK_nutrition_g = rowmean(q611_1- q616_6);

***nutrition (vitamin A and iodized salt advantages, supplementary food);
alpha q613_1- q616_6, asis i;
qui egen MK_nutrition = rowmean(q613_1- q616_6);

*****Mother knowlegde on child development;
alpha q617_1- q618_3, asis i;
qui egen MK_child_development = rowmean(q617_1- q618_3);

* follow code from ecd_gambia_moussa_old.do;
*****Mother knowledge: full set;
foreach var in q601_1 q601_2 q601_3 q601_4 q601_5 q601_6 q601_7 q601_8 q601_9 q601_10 q601_11 q602_1 
		q602_2 q602_3 q602_4 q602_5 q602_6 q602_7 q603_1 q603_2 q603_3 q603_4 q603_5 q603_6 q603_7 q604_1 
		q604_2 q604_3 q605_1 q605_2 q605_3 q605_4 q606_1 q606_2 q606_3 q606_4 q607_1 q607_2 q607_3 q608_1 
		q608_2 q608_3 q608_4 q608_5 q608_6 q608_7 q608_8 q608_9 q609_1 q609_2 q609_3 q609_4 q609_5 q609_6 
		q609_7 q609_8 q610_1 q610_2 q610_3 q610_4 q611_1 q611_2 q612_1 q612_2 q612_3 q612_4 q612_5 q612_6 
		q613_1 q613_2 q613_3 q613_4 q614_1 q614_2 q614_3 q614_4 q614_5 q615_1 q615_2 q615_3 q615_4 q616_1 
		q616_2 q616_3 q616_4 q616_5 q616_6 q617_1 q617_2 q617_3 q617_4 q617_5 q618_1 q618_2 q618_3 {;
	qui recode `var' (1 = 1 )(2 3 =0)(else=.),gen(`var'r);
};

foreach var in q606_5 q607_4 q609_9 q611_3 q611_4 {;
	qui recode `var' (3 = 1 )(2 1 =0)(else=.),gen(`var'r);
}; 

qui egen mother_knowledge1=rowmean(q601_1r q601_2r q601_3r q601_4r q601_5r q601_6r q601_7r q601_8r q601_9r q601_10r q601_11r q602_1r 
	q602_2r q602_3r q602_4r q602_5r q602_6r q602_7r q603_1r q603_2r q603_3r q603_4r q603_5r q603_6r q603_7r q604_1r 
	q604_2r q604_3r q605_1r q605_2r q605_3r q605_4r q606_1r q606_2r q606_3r q606_4r q607_1r q607_2r q607_3r q608_1r 
	q608_2r q608_3r q608_4r q608_5r q608_6r q608_7r q608_8r q608_9r q609_1r q609_2r q609_3r q609_4r q609_5r q609_6r 
	q609_7r q609_8r q610_1r q610_2r q610_3r q610_4r q611_1r q611_2r q612_1r q612_2r q612_3r q612_4r q612_5r q612_6r 
	q613_1r q613_2r q613_3r q613_4r q614_1r q614_2r q614_3r q614_4r q614_5r q615_1r q615_2r q615_3r q615_4r q616_1r 
	q616_2r q616_3r q616_4r q616_5r q616_6r q617_1r q617_2r q617_3r q617_4r q617_5r q618_1r q618_2r q618_3r q606_5r 
	q607_4r q609_9r q611_3r q611_4r);
qui gen mother_knowledge = 100*mother_knowledge1;
qui pca q601_1r q601_2r q601_3r q601_4r q601_5r q601_6r q601_7r q601_8r q601_9r q601_10r q601_11r q602_1r 
	q602_2r q602_3r q602_4r q602_5r q602_6r q602_7r q603_1r q603_2r q603_3r q603_4r q603_5r q603_6r q603_7r q604_1r 
	q604_2r q604_3r q605_1r q605_2r q605_3r q605_4r q606_1r q606_2r q606_3r q606_4r q607_1r q607_2r q607_3r q608_1r 
	q608_2r q608_3r q608_4r q608_5r q608_6r q608_7r q608_8r q608_9r q609_1r q609_2r q609_3r q609_4r q609_5r q609_6r 
	q609_7r q609_8r q610_1r q610_2r q610_3r q610_4r q611_1r q611_2r q612_1r q612_2r q612_3r q612_4r q612_5r q612_6r 
	q613_1r q613_2r q613_3r q613_4r q614_1r q614_2r q614_3r q614_4r q614_5r q615_1r q615_2r q615_3r q615_4r q616_1r 
	q616_2r q616_3r q616_4r q616_5r q616_6r q617_1r q617_2r q617_3r q617_4r q617_5r q618_1r q618_2r q618_3r q606_5r 
	q607_4r q609_9r q611_3r q611_4r;
qui predict p1;
corr p1 mother_knowledge;
drop p1;

* now create alternate versions of knowledge (MK2) that measure % of items marked as "very important," as for "mother_knowledge";
aorder q6*r;
*****Mother knowledge on hygiene and sanitation;
**food hygiene;
qui egen MK2_food_hygiene = rowmean(q601_1r- q602_7r);

**hand washing;
qui egen MK2_hands_washing = rowmean(q603_1r- q604_3r);

**waste management;
qui egen MK2_waste_management = rowmean(q605_1r- q605_4r);

**drinking water management;
qui egen MK2_drinking_water = rowmean(q606_1r- q606_4r q606_5r);

***hygiene and sanitation in general;
qui egen MK2_hygiene_sanitation = rowmean(q601_1r- q606_4r q606_5r);

*****Mother knowlegde on health;
**danger sign;
qui egen MK2_danger_sign = rowmean(q608_1r- q608_9r);

**deworming;
qui egen MK2_deworming = rowmean(q610_1r- q610_4r);

***health issues (deworming and danger sign); 
qui egen MK2_health = rowmean(q608_1r- q608_9r q610_1r- q610_4r);

***health issues in general;
qui egen MK2_health_g = rowmean(q607_1r - q610_4r);

*****Mother knowlegde on nutrition;
**vitamin A advantages;
qui egen MK2_vitamin_A = rowmean(q613_1r- q613_4r);

**Iodized salt advantages;
qui egen MK2_iodized_salt = rowmean(q614_1r- q614_5r);

**Suppementary food;
qui egen MK2_supplementary_food = rowmean(q615_1r- q616_6r);

***nutrition in general;
qui egen MK2_nutrition_g = rowmean(q611_1r- q616_6r);

***nutrition (vitamin A and iodized salt advantages, supplementary food);
qui egen MK2_nutrition = rowmean(q613_1r- q616_6r);

*****Mother knowlegde on child development;
qui egen MK2_child_development = rowmean(q617_1r- q618_3r); 

local knowledge "food_hygiene hands_washing waste_management drinking_water hygiene_sanitation danger_sign deworming
	health health_g vitamin_A iodized_salt supplementary_food nutrition_g nutrition child_development";
foreach x in `knowledge' {;
	lab var MK_`x' "mother knowledge of `x' (1-3 scale, 1 is highest knowledge), endline";
	lab var MK2_`x' "mother knowledge of `x' (% of items marked very important), endline";
};
lab var mother_knowledge1 "mother knowledge (proportion of items marked very important), endline";
lab var mother_knowledge "mother knowledge (% of items marked very important), endline";

* Module 7: Home environment;
* follow code from ecd_gambia_moussa_old.do (line 937-980);
*****Mother interactions with the child;
foreach var in q706_11 q706_21 q706_31 q706_41 q706_51 q706_61 q706_71 {;
	qui replace `var' = 0 if `var' ==2;
};

*egen interaction = rowmean(q706_11 q706_21 q706_31 q706_41 q706_51 q706_61 q706_71);
*pca q706_11 q706_21 q706_31 q706_41 q706_51 q706_61 q706_71;
*predict p1;
*corr p1 interaction;

**Intensity of the interaction;
foreach i in 1 2 3 4 5 6 7 {;
	qui gen interaction_intensity_`i'= q706_`i'1 *(60*q706_`i'2 + q706_`i'3);
	qui replace interaction_intensity_`i'=0 if q706_`i'1==0;
};
*egen interactionintensity = rowmean(interaction_intensity_1 - interaction_intensity_7);
qui egen interaction_intensity = rowtotal(interaction_intensity_1 - interaction_intensity_7), m;
qui gen interaction_play = interaction_intensity - (interaction_intensity_5 + interaction_intensity_7);
*pca interaction_intensity_1 - interaction_intensity_7;
*predict p1;
*corr p1 interaction_intensity interactionintensity;
lab var interaction_intensity "Mother interaction intensity with child (min/day, endline)";
lab var interaction_play "Mother play time with child (min/day, endline)";

*****Child exposure to playing items;
foreach var in q705_1 q705_2 q705_3 q705_4 q705_5 q705_6 q705_7 q705_9 q705_10 q705_11 {;
	qui replace `var' = 0 if `var' ==2;
};
qui egen play_items = rowmean(q705_1 - q705_11);
lab var play_items "% of play items available to child (of 11, endline)";

/*disciplinary actions*/
order q707* q708*;
/*mean 1-3 scale, 1=v. important, 3=not important*/
qui egen disc_nonvlnt_endline=rowmean(q707_1-q707_4);
qui egen disc_psychagg_endline=rowmean(q707_5-q707_8);
qui egen disc_vlnt_endline=rowmean(q707_9-q707_21);
qui egen discsevere_nonvlnt_endline=rowmean(q708_1-q708_4);
qui egen discsevere_psychagg_endline=rowmean(q708_5-q708_8);
qui egen discsevere_vlnt_endline=rowmean(q708_9-q708_21);

/*% of actions marked "very important"*/
qui egen disc_nonvlntvi_endline=anycount(q707_1-q707_4), v(1);
qui egen disc_psychaggvi_endline=anycount(q707_5-q707_8), v(1);
qui egen disc_vlntvi_endline=anycount(q707_9-q707_21), v(1);
qui egen discsevere_nonvlntvi_endline=anycount(q708_1-q708_4), v(1);
qui egen discsevere_psychaggvi_endline=anycount(q708_5-q708_8), v(1);
qui egen discsevere_vlntvi_endline=anycount(q708_9-q708_21), v(1);
qui egen disc_nonvlnti_endline=anycount(q707_1-q707_4), v(1 2);
qui egen disc_psychaggi_endline=anycount(q707_5-q707_8), v(1 2);
qui egen disc_vlnti_endline=anycount(q707_9-q707_21), v(1 2);
qui egen discsevere_nonvlnti_endline=anycount(q708_1-q708_4), v(1 2);
qui egen discsevere_psychaggi_endline=anycount(q708_5-q708_8), v(1 2);
qui egen discsevere_vlnti_endline=anycount(q708_9-q708_21), v(1 2);
qui egen disc_nonvlntt_endline=anycount(q707_1-q707_4), v(1 2 3);
qui egen disc_psychaggt_endline=anycount(q707_5-q707_8), v(1 2 3);
qui egen disc_vlntt_endline=anycount(q707_9-q707_21), v(1 2 3);
qui egen discsevere_nonvlntt_endline=anycount(q708_1-q708_4), v(1 2 3);
qui egen discsevere_psychaggt_endline=anycount(q708_5-q708_8), v(1 2 3);
qui egen discsevere_vlntt_endline=anycount(q708_9-q708_21), v(1 2 3);
foreach y in disc discsevere {;
	foreach x in nonvlnt psychagg vlnt {;
		qui gen `y'_`x'pcti_endline=`y'_`x'i_endline/`y'_`x't_endline;
		qui gen `y'_`x'pctvi_endline=`y'_`x'vi_endline/`y'_`x't_endline;
		drop `y'_`x'vi_endline `y'_`x'i_endline `y'_`x't_endline;
	};
};

lab var disc_nonvlnt_endline "attitude to non-violent discipline (mean 1-3 scale, 1=v. important, 3=not important, endline)";
lab var disc_psychagg_endline "attitude to psych. aggression discipline (mean 1-3 scale, 1=v. important, 3=not important, endline)";
lab var disc_vlnt_endline "attitude to violent discipline (mean 1-3 scale, 1=v. important, 3=not important, endline)";
lab var discsevere_nonvlnt_endline "attitude to non-violent discipline for v. bad behavior (mean 1-3 scale, 1=v. important, 3=not important, endline)";
lab var discsevere_psychagg_endline "attitude to psych. aggression discipline for v. bad behavior (mean 1-3 scale, 1=v. important, 3=not important, endline)";
lab var discsevere_vlnt_endline "attitude to violent discipline for v. bad behavior (mean 1-3 scale, 1=v. important, 3=not important, endline)";
lab var disc_nonvlntpcti_endline "attitude to non-violent discipline (% of actions marked 'very' or 'somewhat' important, endline)";
lab var disc_psychaggpcti_endline "attitude to psych. aggression discipline (% of actions marked 'very' or 'somewhat' important, endline)";
lab var disc_vlntpcti_endline "attitude to violent discipline (% of actions marked 'very' or 'somewhat' important, endline)";
lab var discsevere_nonvlntpcti_endline "attitude to non-violent discipline for v. bad behavior (% of actions marked 'very' or 'somewhat' important, endline)";
lab var discsevere_psychaggpcti_endline "attitude to psych. aggression discipline for v. bad behavior (% of actions marked 'very' or 'somewhat' important, endline)";
lab var discsevere_vlntpcti_endline "attitude to violent discipline for v. bad behavior (% of actions marked 'very' or 'somewhat' important, endline)";
lab var disc_nonvlntpctvi_endline "attitude to non-violent discipline (% of actions marked 'very important', endline)";
lab var disc_psychaggpctvi_endline "attitude to psych. aggression discipline (% of actions marked 'very important', endline)";
lab var disc_vlntpctvi_endline "attitude to violent discipline (% of actions marked 'very important', endline)";
lab var discsevere_nonvlntpctvi_endline "attitude to non-violent discipline for v. bad behavior (% of actions marked 'very important', endline)";
lab var discsevere_psychaggpctvi_endline "attitude to psych. aggression discipline for v. bad behavior (% of actions marked 'very important', endline)";
lab var discsevere_vlntpctvi_endline "attitude to violent discipline for v. bad behavior (% of actions marked 'very important', endline)";

* Module 8: mother's health;
* follow code from ecd_gambia_moussa_old.do (line 922-934);
*****Mother health;
qui recode q804 (0=3 "Most of the time")(1=2 "Occasionally or a moderate amount of th")(2=1 "Some or a little of the time")(3=0 "Rarely or none of the time"), gen(q804_r);
qui recode q807 (0=3 "Most of the time")(1=2 "Occasionally or a moderate amount of th")(2=1 "Some or a little of the time")(3=0 "Rarely or none of the time"), gen(q807_r);
alpha q801- q803 q804_r q805 q806 q807_r q808 q809 q810 q811, asis i;
qui egen mother_health1_endline= rowmean(q801 q802 q803 q804_r q805 q806 q807_r q808 q809 q810 q811);
lab var mother_health1_endline "mother mental health index (0-3 scale, lower is healthier, endline)";
*pca q801 q802 q803 q804_r q805 q806 q807_r q808 q809 q810 q811;
*predict p1;
*egen mother_health_r = cut(p1), group(4);
*tab mother_health_r;
*sum p1;
*corr p1 mother_health1;

*****Mother health option 2 (code from ecd_gambia_moussa_old.do, line 983-999);
foreach var in q801 q802 q803 q805 q806 q808 q809 q810 q811 {;
	qui recode `var' (0 = 1 )(1 2 3 =0),gen(`var'r);
};

foreach var in q804 q807 {;
	qui recode `var' (3 = 1 )(0 1 2 =0)(else=.),gen(`var'r);
};
qui egen mother_health2 = rowmean(q801r q802r q803r q805r q806r q808r q809r q810r q811r q804r q807r);
qui gen mother_health_endline= 100*mother_health2;
pca q801r q802r q803r q805r q806r q808r q809r q810r q811r q804r q807r;
predict p1;
corr p1 mother_health_endline;
drop p1;
lab var mother_health_endline "mother mental health index (% of items 'most of the time', which is bad, endline)";
drop mother_health2;

* Endline skills tests;
* follow code from gambia_ecd_mdatcheck1.do;
/*MDAT FINE MOTOR*/
/*why is there no item 31? (not in questionnaire either)*/
aorder fm_53_76_q??;
qui egen finemotor_endline_miss=rowmiss(fm_53_76_q27-fm_53_76_q43);
qui egen finemotor_endline=rowtotal(fm_53_76_q27-fm_53_76_q43), missing;
tab finemotor_endline_miss, mi;
qui replace finemotor_endline=. if finemotor_endline_miss>=1; /*use complete cases only*/
qui su finemotor_endline;
qui gen zfinemotor_endline=(finemotor_endline-r(mean))/r(sd);
lab var finemotor_endline_miss "MDAT fine motor endline, # of missing items (of 16)";
lab var finemotor_endline "MDAT fine motor endline (of 16)";
lab var zfinemotor_endline "MDAT fine motor endline z-score";

/*version based only on common items between baseline & endline MDAT (see "MDAT crosswalk.xlsx")*/
local fm_common "fm_53_76_q27-fm_53_76_q41"; /*14 common items (#31 missing)*/
qui egen finemotor_comm_endline=rowtotal(`fm_common'), missing;
qui replace finemotor_comm_endline=. if finemotor_endline_miss>=1; /*use complete cases only*/
qui su finemotor_comm_endline;
qui gen zfinemotor_comm_endline=(finemotor_comm_endline-r(mean))/r(sd);
lab var finemotor_comm_endline "MDAT fine motor endline, common baseline/endline items only (of 14)";
lab var zfinemotor_comm_endline "MDAT fine motor endline z-score, common baseline/endline";

/*MDAT LANGUAGE & HEARING*/
/*why is there no item 31? (not in questionnaire either)*/
aorder lh_53_76_q??;
qui egen langhear_endline_miss=rowmiss(lh_53_76_q17-lh_53_76_q51);
qui egen langhear_endline=rowtotal(lh_53_76_q17-lh_53_76_q51), missing;
tab langhear_endline_miss, mi;
qui replace langhear_endline=. if langhear_endline_miss>=1; /*use complete cases only*/
qui su langhear_endline;
qui gen zlanghear_endline=(langhear_endline-r(mean))/r(sd);
lab var langhear_endline_miss "MDAT language & hearing endline, # of missing items (of 26)";
lab var langhear_endline "MDAT language & hearing endline (of 23)";
lab var zlanghear_endline "MDAT language & hearing endline z-score";

/*version based only on common items between baseline & endline MDAT (see "MDAT crosswalk.xlsx")*/
local lh_common "lh_53_76_q17 lh_53_76_q20 lh_53_76_q31-lh_53_76_q43"; /*15 common items*/
qui egen langhear_comm_endline=rowtotal(`lh_common'), missing;
qui replace langhear_comm_endline=. if langhear_endline_miss>=1; /*use complete cases only*/
qui su langhear_comm_endline;
qui gen zlanghear_comm_endline=(langhear_comm_endline-r(mean))/r(sd);
lab var langhear_comm_endline "MDAT language & hearing endline, common baseline/endline items only (of 15)";
lab var zlanghear_comm_endline "MDAT language & hearing endline z-score, common baseline/endline";

/*adjust MDAT scores for age and gender*/
qui gen selected_child_age2=selected_child_age^2;
foreach x in finemotor langhear finemotor_comm langhear_comm {;
	qui xi: reg `x'_endline selected_child_age selected_child_age2 child_female; /*doesn't matter if Y=raw or z score*/
	qui predict e, residuals; 
	qui egen z`x'_adj_endline=std(e);
	drop e;
};
lab var zfinemotor_adj_endline "MDAT fine motor endline z-score (age-adjusted)";
lab var zlanghear_adj_endline "MDAT language & hearing endline z-score (age-adjusted)";
lab var zfinemotor_comm_adj_endline "MDAT fine motor endline z-score (age-adjusted), common baseline/endline items only";
lab var zlanghear_comm_adj_endline "MDAT language & hearing endline z-score (age-adjusted), common baseline/endline items only";

/*adjust MDAT scores by age, with two differences from above:
	1) non-parametric adjustment
	2) adjust both mean and variance by age
	--code follows IRT normalization in gambia_ecd_clean3.do, which follows Pam Jervis analysis for Gambia ECD 0-3 
		data. For example, see ecd_gambia_ses.do, lines 415-436.*/
foreach x in finemotor langhear {;
	/*variance conditional on age*/
	qui lpoly `x'_endline selected_child_age, at(selected_child_age) gen(mu) nograph;
	qui gen r=`x'_endline-mu; /*residual and its square*/
	qui gen r2=r^2;
	qui lpoly r2 selected_child_age, at(selected_child_age) gen(var) nograph;
	qui gen std = sqrt(var);

	/*z-score adjusted for age*/
	if ("`x'"=="finemotor") local xx "fm";
	if ("`x'"=="langhear") local xx "lh";	
	qui gen z`xx'_adj_endline=(`x'_endline-mu)/std;
	drop mu r r2 std var;
};		
lab var zfm_adj_endline "MDAT fine motor endline z-score (age-adjusted, both mean & sd)";
lab var zlh_adj_endline "MDAT language & hearing endline z-score (age-adjusted, both mean & sd)";
		
/*Subsets of MDAT items (53-76 months) that correspond to experimental curriculum (GOALS)*/
/*See "Curriculum vs. MDAT Matrix.xlsx" for details*/
/*fine motor skills groups*/
/*note that blocks, draw, order items are common between baseline and endline*/
qui egen finemotor_blocks_endline=rowtotal(fm_53_76_q30 fm_53_76_q35 fm_53_76_q36);
qui gen finemotor_blocks_pct_endline=finemotor_blocks_endline/3;
qui egen finemotor_draw_endline=rowtotal(fm_53_76_q27 fm_53_76_q32-fm_53_76_q34);
qui gen finemotor_draw_pct_endline=finemotor_draw_endline/4;
qui egen finemotor_order_endline=rowtotal(fm_53_76_q37 fm_53_76_q38);
qui gen finemotor_order_pct_endline=finemotor_order_endline/2;
qui gen finemotor_fold_endline=fm_53_76_q42;

/*language and hearing skills groups*/
/*note that sentence, colors, count items are common between baseline and endline. Create common version of name items below also.*/
qui egen langhear_blocks_endline=rowtotal(lh_53_76_q46-lh_53_76_q48);
qui gen langhear_blocks_pct_endline=langhear_blocks_endline/3;
qui gen langhear_name_endline=lh_53_76_q20;
qui egen langhear_name_comm_endline=rowtotal(lh_53_76_q20 lh_53_76_q41-lh_53_76_q43);
qui gen langhear_name_comm_pct_endline=langhear_name_comm_endline/4;
qui gen langhear_sentence_endline=lh_53_76_q17;
qui egen langhear_books_endline=rowtotal(lh_53_76_q49-lh_53_76_q51);
qui gen langhear_books_pct_endline=langhear_books_endline/3;
qui egen langhear_count_endline=rowtotal(lh_53_76_q33-lh_53_76_q35);
qui gen langhear_count_pct_endline=langhear_count_endline/3;
qui egen langhear_colors_endline=rowtotal(lh_53_76_q37-lh_53_76_q40);
qui gen langhear_colors_pct_endline=langhear_colors_endline/4;

/*combined skills groups (note that these overlap with some from individual categories*/
qui gen mdat_blocks_endline=finemotor_blocks_endline+langhear_blocks_endline;
qui gen mdat_blocks_pct_endline=mdat_blocks_endline/6;

lab var finemotor_blocks_endline "MDAT fine motor skills (endline) play with blocks (of 3)";
lab var finemotor_blocks_pct_endline "MDAT fine motor skills (endline) play with blocks (% of 3)";
lab var finemotor_draw_endline "MDAT fine motor skills (endline) draw lines & shapes (of 4)";
lab var finemotor_draw_pct_endline "MDAT fine motor skills (endline) draw lines & shapes (% of 4)";
lab var finemotor_order_endline "MDAT fine motor skills (endline) order rows of items (of 2)";
lab var finemotor_order_pct_endline "MDAT fine motor skills (endline) order rows of items (% of 2)";
lab var finemotor_fold_endline "MDAT fine motor skills (endline) folding paper";
lab var langhear_blocks_endline "MDAT language skills (endline) play with blocks (of 3)";
lab var langhear_blocks_pct_endline "MDAT language skills (endline) play with blocks (% of 3)";
lab var langhear_name_endline "MDAT language skills (endline) can say name";
lab var langhear_name_comm_endline "MDAT language skills (endline) can say name (of 4), common baseline/endline items";
lab var langhear_name_comm_pct_endline "MDAT language skills (endline) can say name (% of 4), common baseline/endline items";
lab var langhear_sentence_endline "MDAT language skills (endline) speaks in clear sentences";
lab var langhear_books_endline "MDAT language skills (endline) open books (of 3)";
lab var langhear_books_pct_endline "MDAT language skills (endline) open books (% of 3)";
lab var langhear_count_endline "MDAT language skills (endline) counting (of 3)";
lab var langhear_count_pct_endline "MDAT language skills (endline) counting (% of 3)";
lab var langhear_colors_endline "MDAT language skills (endline) name colors (of 4)";
lab var langhear_colors_pct_endline "MDAT language skills (endline) name colors (% of 4)";
lab var mdat_blocks_endline "MDAT language & fine motor skills (endline) play with blocks (of 6)";
lab var mdat_blocks_pct_endline "MDAT language & fine motor skills (endline) play with blocks (% of 6)";

* Anthropometrics: endline;
* follow code from ecd_gambia_moussa_old.do;
qui egen child_height_endline = rowmean(q1603_1 q1603_2 q1603_3); //height  
qui egen child_weight_endline = rowmean(q1604_1 q1604_2 q1604_3); // weight 
qui egen child_armcirm_endline = rowmean(q1605_1 q1605_2 q1605_3); // brachial cirmcum
qui gen childBMI_endline = child_weight_endline*10000/(child_height_endline)^2; // 
lab var child_height_endline "height (cm)";
lab var child_weight_endline "weight (kg)";
lab var child_armcirm_endline "brachial circumference (cm), endline"; 
ren child_armcirm child_armcirm_baseline;
foreach x in height weight {;
	ren child_`x' child_`x'_baseline;
	lab var child_`x'_endline "child `x', endline";
};
foreach x in BMI BMI_status BMI_status_rec BMI_under BMI_over {;
	ren child`x' child`x'_baseline;	
};
lab var childBMI_endline "child body mass index, endline";

ren childBMI_endline childBMI; /*rename so that code in gambia_ecd_clean1_aux.do works*/
label drop bmi;
run `do'\gambia_ecd_clean1_aux_endline;	
foreach x in BMI BMI_status BMI_status_rec BMI_under BMI_over {;
	ren child`x' child`x'_endline;	
};

/*get height/weight for age z-scores (WHO 2006): age 5 and under*/
/*note that age in months only valid for children 5 and under*/
/*cap z-scores at +/-6*/
*qui gen child_age_mths_el=child_age_mths+17; /*endline was 17 months after baseline*/
zscore06, a(selected_child_age) s(child_gender_modified) h(child_height_endline) w(child_weight_endline);
foreach x in haz waz whz bmiz {;
	ren `x'06 `x'_endline;
};
foreach x in haz waz whz {;
	qui replace `x'_endline=. if selected_child_age>60|`x'_endline==99;
};
qui replace bmiz_endline=. if haz_endline==.|waz_endline==.;

/*get height for age z-scores (WHO 2006): over age 5*/
/*based on WHO 2007, downloaded from http://www.who.int/growthref/who2007_height_for_age/en/,
	http://www.who.int/growthref/hfa_boys_z_WHO2007_exp.txt,
	http://www.who.int/growthref/hfa_girls_z_WHO2007_exp.txt*/
qui gen agemonths_approx=floor(selected_child_age);
qui replace agemonths_approx=61 if selected_child_age>60 & selected_child_age<61 & selected_child_age!=.;
qui merge m:1 child_gender_modified agemonths_approx using `cleandata'\haztemp;
drop _merge;
qui replace haz_endline=(child_height_endline - m)/stdev if agemonths_approx>=61;
foreach x in haz waz whz bmiz {;
	qui replace `x'_endline=-6 if `x'_endline<-6 & `x'_endline!=.;
	qui replace `x'_endline=6 if `x'_endline>6 & `x'_endline!=.;
};
drop agemonths_approx m stdev;
qui drop if child_gender_mismatch==.;

/*indicator for stunted growth: height-for-age <-2 s.d., see http://www.who.int/nutrition/nlis_interpretation_guide.pdf p. 1 for definition*/
foreach x in baseline endline {;
	qui gen stunted_`x'=.;
	qui replace stunted_`x'=1 if haz_`x'<-2 & haz_`x'!=.;
	qui replace stunted_`x'=0 if haz_`x'>=-2 & haz_`x'!=.;
	lab var stunted_`x' "stunted growth (height-for-age z-score < -2, `x')";
};

* create household and child IDs;
qui egen hhid=group(settlement_code household_number);
sort hhid child_age_mths_dob selected_child_age;
qui egen child_id=seq();
lab var hhid "Household identifier (based on settlement and HH #, added in data cleaning)";
lab var child_id "Child identifier (added in data cleaning)";

/*
qui drop if child_age==.;
qui drop if (child_age<36|child_age>72) & child_age_unit==2; /*age in months*/
qui drop if (child_age<3|child_age>6) & child_age_unit==1; /*age in years*/
count;
*/

* FLAG IMPLEMENTATION PROBLEMS;
* Source: "Evaluation chapter.docx", p. 27-29;
/*first get settlement names from 2003 Census*/
* merge with 2003 Census;
ren settlement_code geocode;
if (mode==1) cd "C:\Users\MY PC\Documents\Research\Program_Eval\Gambia";
else if (mode==2|mode==3) cd "C:\Users\pugatcht\Documents\Research\Program_Eval\Gambia";
qui merge m:1 geocode using "hardship_allowance\data\2003 Census\census_settlement.dta", keepusing(settlement);
qui drop if _merge==2;
drop _merge;
ren geocode settlement_code;

* flag sites with implementation issues (see label definition for key to codes);
/*note that "Evaluation chapter.docx" referes to ECD providers/teachers as "facilitators," but I use term "teachers" here*/
qui gen implement_flag=0;
qui replace implement_flag=1 if settlement=="TOUBA"|settlement=="ARANGKOLY KUNDA"|settlement=="TINTIBA"|
	settlement=="DEMBA KUNDA KOTO (DEMBA KUNDA BAHAWA)"|settlement=="FARATO"|settlement=="TENKOLY (MADINA DEMBA)"|
	settlement=="KARINORR"|settlement=="BERY NABEH"; /*Garawol Kuta missing. Bery Nabeh listed as Barnabeh in report.*/
qui replace implement_flag=2 if settlement=="SERE KUNDA NDING";
qui replace implement_flag=3 if settlement=="BATABUTU KANTORA";
qui replace implement_flag=4 if settlement=="GIKIS DANDON"|settlement=="KAMBAGAL"; /*Jambar Sanneh missing. Touba also on this list, but already flagged*/
qui replace implement_flag=5 if settlement=="MANOKOTO KEITA (SINCHU KEITA)"|settlement=="SARE SADA (MADINA SADA)"; /*Sare Sada listed as Sare Dada in report*/
qui replace implement_flag=6 if settlement=="JARROL";
qui replace implement_flag=7 if settlement=="SARE DEMBA DAADO"; /*Jarrol also on this list. Sare Demba Daado listed as Sare Demba Eyour in report. Sare Demba Bubu Bah also appears in dataset.*/
qui replace implement_flag=8 if settlement=="TOUBA"|settlement=="BUNIADU (BAKAKUL)";
qui gen implement_problem=(implement_flag>0);
qui gen shednotbuilt=(implement_flag==8);
 
lab def implement_flag 	0 "none reported" 
						1 "absent or sick teachers(s)"
						2 "lack of community support"
						3 "lack of communication between community & implementers"
						4 "lack of communication from teachers"
						5 "lack of trained teachers"
						6 "lack of materials"
						7 "lack of food for children"
						8 "lack of shed";
lab val implement_flag implement_flag;
lab var implement_flag "type of implementation problem";
lab var implement_problem "implementation problem reported";
lab var shednotbuilt "shed not built";

* cleanup and save;
if (mode==1) qui cd "C:\Users\MY PC\Box Sync\ECD - 3 to 6 - Community vs Annexes";
else if (mode==2|mode==3) qui cd "C:\Users\pugatcht\Box Sync\ECD - 3 to 6 - Community vs Annexes";
qui compress;
lab data "Gambia ECD age 3-6, merged baseline & endline, cleaned in gambia_ecd_clean1.do";
qui save `cleandata'\cleanv1temp, replace;

/*merge with MDAT scores from item response theory model (see gambia_ecd_clean3.do)*/
qui merge 1:1 child_id using `cleandata'\irt_output_clean3;
drop _merge;

qui compress;
lab data "Gambia ECD age 3-6, merged baseline & endline, cleaned in gambia_ecd_clean1.do";
qui save `cleandata'\ECD_3to6_Gambia_cleanv1, replace;

erase `cleandata'\ecdtemp.dta;
erase `cleandata'\haz_girls.dta;
erase `cleandata'\haztemp.dta;
erase `cleandata'\cleanv1temp.dta;
local end=`"$S_TIME"'; 
di "`start'";
di "`end'";
